<template>
  <div>
    <div>
      <el-button type="primary" @click="addBtn('editSystemConfigFrom')">增加</el-button>
    </div>
    <el-table :data="dataList">
      <el-table-column prop="id" label="编号" width="80"/>
      <el-table-column prop="name" label="分类名称" width="180"/>
      <el-table-column prop="alias" label="别名" width="180"/>
      <el-table-column prop="template_name" label="模板" width="180"/>
      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="rowEditBtn(scope.$index, scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="rowDeleteBtn(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="增加系统配置" :visible.sync="dialogFormVisible">
      <el-form ref="systemEditUserFrom" label-suffix="：" label-position="right" :model="editItem" :rules="rules"
               label-width="120px">
        <el-form-item prop="name" label="分类名称">
          <el-col :span="15">
            <el-input type="text" v-model="editItem.name"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item prop="email" label="别名">
          <el-col :span="15">
            <el-input type="text" v-model="editItem.alias"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item prop="template_name" label="模板">
          <el-col :span="15">
            <el-input type="text" v-model="editItem.template_name"></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveBtn('systemEditUserFrom')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import {archiverCategoryList, archiverCategorySave,archiverCategoryRemove}  from '../../services/index';
  import remoteValidatorMixin from '../../components/mixin/RemoteValidatorMixin.js';
  export default {
    /* name varchar(40) not null comment '分类名称',
     alias varchar(40) not null comment '分类别名',
     template_name varchar(40) not null comment '模板名称',
     url varchar(40) not null comment 'url',
     parent_id int(10) unsigned null comment '上级编号',
     */
    mixins: [remoteValidatorMixin],
    data () {
      return {
        dataItem: 'editItem',
        list: [],
        editItem: {
          name: '',
          id: 0,
          alias: '',
          template_name: ''
        },
        dialogFormVisible: false
      }
    },
    computed: {
      dataList: function () {
        if (this.list && this.list.data) {
          return this.list.data;
        } else {
          return null;
        }
      }
    },

    async created () {

      let {success, data} = await archiverCategoryList(0);
      if (success) {
        this.list = data;
      }
    },
    methods: {
      rowEditBtn (idx, data) {
        this.editItem = _.clone(data);
        this.dialogFormVisible = true;
      },
      addBtn () {
        this.editItem = {name: '', id: 0, alias: '', template_name: ''};
        this.dialogFormVisible = true;
      },
      async saveBtn (formName) {
        let self = this;
        self.remoteValidate(this, formName, archiverCategorySave).then(function (data) {
          if (data) {
            let key = _.findKey(self.list.data, {'id': data.id});
            if (key) {
              _.assign(self.list.data[key], data);
            } else {
              self.list.data.push(data);
            }
            self.dialogFormVisible = false;
          }
        }, function (data) {
          console.log('error');
        });
      },
      async rowDeleteBtn (idx, data) {
        let {success} = await archiverCategoryRemove(data.id);
        if (success) {
          this.list.data.splice(idx, 1);
          this.$message({
            showClose: true,
            message: '删除成功'
          });
        } else {
          this.$message({
            showClose: true,
            message: '删除失败'
          });
        }
      }
    }
  }

</script>
